class Integer < Numeric
  def to_s_time
    time = self.to_i / 1000
    [time/3600, time/60 % 60, time % 60].map{|t| t.to_s.rjust(2,'0')}.join(':')
  end
end

class Video
  attr_accessor :name, :episode, :current_time, :duration, :path, :scrobbled
  def initialize(path, current_time, duration)
    raw_name = path.split('/').last()
    pretty_name = raw_name.gsub(/\[[^\]]*\]/,'').gsub(/\(.*\)/,'').gsub(/DVD/,'')   \
                  .gsub(/.mkv/,'').gsub(/.avi/,'').gsub(/[-_\.]/,' ').gsub(/\s*$/,'').gsub(/^\s/,'')

    @episode = (/[0-9]+$/.match(pretty_name))[0]
    @name = pretty_name.gsub(/[\s]*[0-9]+$/, '')
    
    @path = path
    @current_time = current_time
    @duration = duration
    @scrobbled = false
  end
  
  def ==(object)
    object.class = self.class and object.path == self.pa
  end
  
  def to_s
    @name + ' ' + @episode + ' ' + @current_time.to_s_time + " / " + @duration.to_s_time
  end
  
  def ready_for_scrobble?
    @current_time.to_f / @duration.to_f >= 0.8
  end
  
  def scrobbled?
    scrobbled
  end
  
end